package com.example.game;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.servlet.ModelAndView;

import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

@Controller
public class GameController {

    final static List<Integer> integers = IntStream.rangeClosed(1, 100)
            .boxed()
            .collect(Collectors.toList());
    final Set<Integer> usedNums = new HashSet<>();
    final Integer times = 4;
    Integer i = 0;

    @GetMapping("next")
    public ModelAndView next() {
        if (i >= times) {
            clear();
            i = 0;
        } else {
            i++;
        }
        int index = new Random().nextInt(integers.size());
        Integer num = integers.get(index);
        while (Objects.isNull(num) || usedNums.contains(num)) {
            index = new Random().nextInt(integers.size());
            num = integers.get(index);
        }
        System.out.println(num);
        usedNums.add(num);
        ModelAndView modelAndView = new ModelAndView("picture");
        modelAndView.addObject("imgNo", num + ".png");
        return modelAndView;
    }

    private void clear() {
        System.out.println();
        System.err.println("-----------------------------");
        usedNums.clear();
    }

}
